﻿// CLandsatRadiationDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "LandsatRadiationDialog.h"
#include "afxdialogex.h"
#include "ckLandsat05.h"


// CLandsatRadiationDialog 对话框

IMPLEMENT_DYNAMIC(CLandsatRadiationDialog, CDialogEx)

CLandsatRadiationDialog::CLandsatRadiationDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_LANDSAT_RADIATION, pParent)
	, m_strRasterPath(_T(""))
	, m_strTxtPath(_T(""))
{	
	//m_strRasterPath = "F:\\Project\\项目实施\\TJ-数据质量检查-2018\\样例数据\\RAW\\LT05_L1TP_132016_20000320_20161214_01_T1";
	m_strRasterPath = "E:\\Document\\2-项目实施\\TJ-数据质量检查-2018\\样例数据\\RAW\\LT05_L1TP_132016_20000320_20161214_01_T1";
	m_strTxtPath = "E:\\temp\\radiation.txt";
	//m_pLandsat = NULL;
}

CLandsatRadiationDialog::~CLandsatRadiationDialog()
{
}

void CLandsatRadiationDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_INPATH, m_strRasterPath);
	DDX_Text(pDX, IDC_EDIT_OUTPATH, m_strTxtPath);
	DDX_Control(pDX, IDC_PROGRESS1, m_progressbar);
}


BEGIN_MESSAGE_MAP(CLandsatRadiationDialog, CDialogEx)
	ON_BN_CLICKED(IDOK, &CLandsatRadiationDialog::OnBnClickedOk)
	ON_BN_CLICKED(IDC_BTN_INPATH, &CLandsatRadiationDialog::OnBnClickedBtnInpath)
	ON_BN_CLICKED(IDC_BTN_OUTPATH, &CLandsatRadiationDialog::OnBnClickedBtnOutpath)
END_MESSAGE_MAP()


// CLandsatRadiationDialog 消息处理程序

BOOL CLandsatRadiationDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	UpdateData(FALSE);

	UpdateData(TRUE);


	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CLandsatRadiationDialog::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码

	UpdateData(TRUE);

	ck::data::LandsatRadiation* plr = NULL;
	plr = Compute();
	if (plr == NULL)
	{
		AfxMessageBox("计算失败");
	}

	RESULTCODE rc;
	rc = plr->Write(m_strTxtPath);
	if (rc != RC_SUCCESS)
	{
		AfxMessageBox("输出结果失败");
	}
	else
	{
		AfxMessageBox("计算完成");
	}
	plr->Release();


	//CDialogEx::OnOK();
}


void CLandsatRadiationDialog::OnBnClickedBtnInpath()
{
	// TODO: 在此添加控件通知处理程序代码
	char szPath[MAX_PATH]; //存放选择的目录路径 
	CString str;

	ZeroMemory(szPath, sizeof(szPath));
	//strcpy(szPath, "E:\\Project\\Apps\\zChecker\\Data\\Database\\gdb\\checker.gdb");
	BROWSEINFO bi;
	bi.hwndOwner = AfxGetMainWnd()->m_hWnd;
	bi.pidlRoot = NULL;
	bi.pszDisplayName = szPath;
	bi.lpszTitle = "请选择GDB目录";
	bi.ulFlags = 0;
	bi.lpfn = NULL;// SHBrowseForFolderCallbackProc;
	bi.lParam = 0;
	bi.iImage = 0;

	LPITEMIDLIST lp = SHBrowseForFolder(&bi);
	if (lp && SHGetPathFromIDList(lp, szPath))
	{
		m_strRasterPath = szPath;
		UpdateData(FALSE);
	}
}


void CLandsatRadiationDialog::OnBnClickedBtnOutpath()
{
	// TODO: 在此添加控件通知处理程序代码
	const char*  szFilter = "txt文件 (*.txt)|*.txt";
	CString filePath("");
	CFileDialog dlg(FALSE, "txt", NULL, OFN_HIDEREADONLY, szFilter);

	int newLayerCunt = 0;
	if (dlg.DoModal() == IDOK)
	{
		m_strTxtPath = dlg.GetPathName();
		UpdateData(FALSE);
	}
}

ck::data::LandsatRadiation* CLandsatRadiationDialog::Compute()
{
	UpdateData(TRUE);

	ck::data::Landsat* pLandsat = NULL;
	ck::data::RasterFactory* pRasterFactory = ck::data::RasterFactory::GetInstance();
	RESULTCODE rc = RC_FAILURE;

	//pLandsat = pRasterFactory->CreateLandsat05();
	pLandsat = new ck::data::Landsat05();
	pLandsat->SetPath(m_strRasterPath);
	rc = pLandsat->Open();
	if (rc != RC_SUCCESS)
	{
		pLandsat->Release();
		return NULL;
	}

	ck::data::LandsatRadiation* plr = NULL;
	plr = new ck::data::LandsatRadiation();
	rc = plr->Create(pLandsat);
	if (rc != RC_SUCCESS)
	{
		pLandsat->Release();
		return NULL;
	}

	double cc = pLandsat->ComputeCloudCoverage();
	plr->SetCloudCoverage(cc);
	
	ck::data::Raster* pRaster = NULL;
	ck::data::RasterBand* pBand = NULL;
	ck::data::RasterRadiation* lsr = plr->GetRadiation();
	ck::data::RasterRadiation* brs = plr->GetBandRadiation();
	ck::data::RasterRadiation* br = NULL;
	z_uint32 nBands = plr->GetBandCount();

	m_progressbar.SetRange(0, nBands+1);
	
	z_uint32 i = 0;
	for (i = 0; i < nBands; i++)
	{
		TRACE1("迭代：%d\n", i);
		m_progressbar.SetPos(i + 1);

		pRaster = pLandsat->OpenRaster(i);
		if (pRaster == NULL)
		{
			continue;
		}
		pBand = pRaster->GetBand(0);
		if (pBand)
		{
			ck::processor::raster::RasterBandRadiationProcessor rrp;
			rrp.SetBand(pBand);
			rrp.Execute();
			br = rrp.GetData();

			brs[i] = *br;
			*lsr += *br;
		}

		*lsr /= nBands;
		pRaster->Release();

		m_progressbar.SetPos(i + 1);
	}
	m_progressbar.SetPos(i + 1);

	pLandsat->Release();

	return plr;
}